﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Reflection;
#if NETFX_CORE
using Windows.UI.Xaml;
using Windows.Foundation;
using WindowsPoint = global::Windows.Foundation.Point;
#else
    using WindowsPoint = global::System.Windows.Point;
#endif
namespace ArchiSimple.Windows
{
    public static class MethodExtentions
    {
        public static ObservableCollection<T> ToObservable<T>(this IEnumerable<T> collection)
        {
            return new ObservableCollection<T>(collection);
        }

        public static int IndexOf<T>(this IEnumerable<T> collection, Predicate<T> predicate)
        {
            int index = 0;

            foreach (var item in collection)
            {
                if (predicate(item))
                {
                    return index;
                }

                index++;
            }

            return -1;
        }

        public static WindowsPoint Substract(this WindowsPoint p1, WindowsPoint p2)
        {
            return new WindowsPoint
            {
                X = p1.X - p2.X,
                Y = p1.Y - p2.Y,
            };
        }

        public static IEnumerable<PropertyInfo> GetProperties(this Type t)
        {
#if NETFX_CORE
            return t.GetRuntimeProperties();
#else
            return t.GetProperties();
#endif
        }

#if NETFX_CORE
        public static object FindResource(this FrameworkElement element, object resourceKey)
        {
            var currentElement = element;

            while (currentElement != null)
            {
                if (currentElement.Resources.ContainsKey(resourceKey))
                {
                    var resource = currentElement.Resources[resourceKey];
                    if (resource != null)
                    {
                        return resource;
                    }
                }

                currentElement = currentElement.Parent as FrameworkElement;
            }
            if (Application.Current.Resources.ContainsKey(resourceKey))
            {
                return Application.Current.Resources[resourceKey];
            }
            else
            {
                return null;
            }
        }

#endif
    }
}
